package cn.devices.mapper;

import cn.devices.entity.TbAdm;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.List;

import static org.apache.ibatis.type.JdbcType.DATE;

/**
 * <p>
 * 管理员表 Mapper 接口
 * </p>
 *
 * @author 秋水浮萍
 * @since 2021-02-07
 */
@Repository
public interface TbAdmMapper {

    @Select("select * from tb_adm where admin_id=#{admId}")
    TbAdm getByAdmId(Integer admId);

    @Select("select * from tb_adm where login_name={loginName} and login_pass={loginPass}")
    TbAdm getByUP(String loginName, String loginPass);

    @Select("select * from tb_adm where login_name=#{loginName}")
    TbAdm getByLoginName(String loginName);


    @Insert("insert into tb_adm(admin_name, login_name, login_pass, last_login, admin_tel, disabled)" +
            " value (#{adminName},#{loginName},#{loginPass},#{lastLogin,},#{adminTel},0)")
    int tbAdmAdd(TbAdm tbAdm);

    @Update("update tb_adm set  admin_tel=#{adminTel} where login_name=#{loginName}")
    int tbAdmUpdate(TbAdm tbAdm);

    @Select("select count(*) from tb_adm  where login_name!=#{loginName} and admin_tel=#{adminTel}")
    int tbAdmTelCount(TbAdm tbAdm);

    @Select("select login_pass from tb_adm where admin_id=#{admId} ")
    String loginPassCorrect(Integer admId);

    @Update("update tb_adm set login_pass=#{loginPassNew} where admin_id=#{admId}")
    int loginPassUpdate(Integer admId, String loginPassNew);

    @Update("update tb_adm set  disabled=0 where admin_id=#{adminId}")
    int tbAdmDeleteFalse(Integer adminId);

    @Delete("delete  from tb_adm where admin_id=#{adminId}")
    int tbAdmDeleteTrue(Integer adminId);

    @Select("<script>" +
            "select count(*) from tb_adm " +
            "<if test='loginName != null and loginName!=\"\"'>" +
            " where login_name=#{loginName}" +
            "</if>" +
            "<if test='adminTel != null and adminTel!=\"\"'>" +
            " or admin_tel=#{adminTel}" +
            "</if>" +
            "</script>")
    int confirmLoginName(String loginName, String adminTel);

    @Update("update tb_adm set last_login=#{lastLogin} where admin_id=#{adminId}")
    int lastLogin(String lastLogin, String adminId);

    @Select("select * from tb_adm where disabled=0 order by disabled,admin_id")
    List<TbAdm> tbAdmList();


}
